package com.edu.fjnu.eshop.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.fjnu.eshop.utils.CookieUtils;

@WebFilter(filterName = "LoginFilter")
public class LoginFilter implements Filter {
    public static final String login_page = "/System_management/Login.html";
    public static final String index_page = "/index.html";
    public static final String error_page = "/401.html";
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        HttpSession session = request.getSession();
        String user_id = (String)session.getAttribute("user_id");
        String user_type = (String)session.getAttribute("user_type");

        Cookie cookie_user_id = CookieUtils.getCookie(request, "user_id");
        Cookie cookie_user_type = CookieUtils.getCookie(request, "user_type");


        String currentURL = request.getRequestURI();
        String ctxPath = request.getContextPath();

        //闄ゆ帀椤圭洰鍚嶇О鏃惰闂〉闈㈠綋鍓嶈矾寰�
        String targetURL = currentURL.substring(ctxPath.length());



        System.out.println("targetURL:" + targetURL + "\nctxPath:" + ctxPath + "\ncurrentURL:" +currentURL);
        if (targetURL.indexOf("Login.html") > 0 || targetURL.equals("/")) {
            System.out.println("login request");
            chain.doFilter(request, response);
        } else if (targetURL.indexOf("/Main/LoginCheck") !=-1) {  //璺宠繃鐧诲綍鎿嶄綔
            chain.doFilter(request, response);
        } else if (targetURL.indexOf("/System_Disable") ==0 || targetURL.indexOf("/System_Company") ==0 ||targetURL.indexOf("/System_Media") ==0) {

            if (user_id == null) {
                response.sendRedirect(login_page);
                return;
            }
            if (cookie_user_id == null || cookie_user_type==null ) {
                response.sendRedirect(login_page);
                return;
            }
            String request_type = "";
            if (targetURL.indexOf("/System_Disable") ==0) {
                request_type = "A";//璇锋眰绫诲瀷
            } else if (targetURL.indexOf("/System_Company") ==0) {
                request_type = "B";
            } else {
                request_type = "C";
            }
            boolean f1 = false, f2 = false;
            //鍒ゆ柇cookie璺焥ession鏄惁鐩哥瓑
            if (user_id.equals(cookie_user_id.getValue())) {
                if (user_type.equals(cookie_user_type.getValue()) && request_type.equals(cookie_user_type.getValue())) {
                    chain.doFilter(request, response);
                } else {
                    response.sendRedirect(error_page);
                    return;
                }
            } else {
                response.sendRedirect(login_page);
                return;
            }


        }
        //鏈煡鐨勮姹�
        else {
            chain.doFilter(request, response);
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
